import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {RoleGroup} from "../../../../model/RoleGroup";
import {ManageService} from "../../../../services/manage.service";
import {AlertService} from "../../../../services/alert.service";
import {NzModalService} from "ng-zorro-antd/modal";
import {GweditComponent} from "../gwedit/gwedit.component";
import {GwcdsqeditComponent} from "../gwcdsqedit/gwcdsqedit.component";

@Component({
  selector: 'app-gwlist',
  templateUrl: './gwlist.component.html',
  styleUrls: ['./gwlist.component.css']
})
export class GwlistComponent implements OnInit {
  rolelist: Array<RoleGroup>;

  constructor(private manageservice: ManageService,
              private alertService: AlertService,
              private modal: NzModalService
    , private viewContainerRef: ViewContainerRef) {
    this.rolelist = new Array<RoleGroup>()
  }

  ngOnInit(): void {
    this.getRoleList()
  }

  getRoleList() {
    this.manageservice.getRoleList()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.rolelist = new Array<RoleGroup>();
          this.rolelist = data.list;
        }

      })
  }

  add() {
    const modalwindow = this.modal.create({
      nzTitle: '岗位信息编辑',
      nzContent: GweditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: 'add',

      },

      nzOnOk: () => new Promise(resolve => {
        // modalwindow.getContentComponent().save();
        let temp = modalwindow.getContentComponent().role;
        if (!temp.name) {
          this.alertService.showWarning("岗位名称必填")
          resolve(false);
        } else {
          modalwindow.getContentComponent().save();
          setTimeout(() => {

            resolve(true);
            this.getRoleList();
          }, 2000)
        }


      })
    })
  }

  update(id: string) {
    const modalwindow = this.modal.create({
      nzTitle: '岗位信息编辑',
      nzContent: GweditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: 'update',
        id: id
      },

      nzOnOk: () => new Promise(resolve => {
        // modalwindow.getContentComponent().save();
        let temp = modalwindow.getContentComponent().role;
        if (!temp.name) {
          this.alertService.showWarning("岗位名称必填")
          resolve(false);
        } else {
          modalwindow.getContentComponent().save();
          setTimeout(() => {

            resolve(true);
            this.getRoleList();
          }, 2000)
        }


      })
    })
  }

  delete(id: string) {
    this.manageservice.deleteRole(id)
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.getRoleList();
        }
        else {
          this.alertService.showError('删除异常，请重试');
        }
      })
  }

  toCdedit(id: string) {
    const modalwindow = this.modal.create({
      nzTitle: '菜单信息编辑',
      nzContent: GwcdsqeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        id: id
      },

      nzOnOk: () => new Promise(resolve => {
        modalwindow.getContentComponent().save();
        setTimeout(() => {
          resolve(true);
          this.getRoleList();
        }, 2000)
      })
    })
  }

}
